package com.nutstudio.feilproject.acitivty;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nutstudio.feilproject.R;
import com.nutstudio.feilproject.bean.TestBean;
import com.nutstudio.feilproject.manager.net_tools.UploadListener;
import com.nutstudio.feilproject.presenter.MainPresenter;
import com.nutstudio.feilproject.view.TestView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


public class MainActivity extends BaseActivity implements TestView {


    @BindView(R.id.msg_tv)
    TextView msgTv;
    private ProgressDialog progressDialog;

    private MainPresenter mainPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        mainPresenter = new MainPresenter(this, this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        findViewById(R.id.upload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                testUpload();
            }
        });
    }

    private void testUpload() {
       // String path = Environment.getExternalStorageDirectory() + File.separator + "test3.png";
       // uploadSingle(new File(path));

    }
    private Observable<List<File>> getAllFileList() {
       return Observable.create(new ObservableOnSubscribe<List<File>>() {
            @Override
            public void subscribe(ObservableEmitter<List<File>> emitter) throws Exception {
                List<File> fileList=new ArrayList<>();
                String path = Environment.getExternalStorageDirectory() + File.separator + "test.png";
                String path2 = Environment.getExternalStorageDirectory() + File.separator + "test2.png";

                fileList.add(new File(path));
                fileList.add(new File(path2));
                emitter.onNext(fileList);
            }
        });
    }
    private void uploadSingle(File file) {
        progressDialog.show();
        String path2 = Environment.getExternalStorageDirectory() + File.separator + "test2.png";
        mainPresenter.uploadFileS("测试121", file, new UploadListener() {
            @Override
            public void onRequestProgress(long bytesWritten, long contentLength) {
                Log.d("test",bytesWritten+":"+contentLength);
                progressDialog.setProgress((int) (bytesWritten*1.0/contentLength*100));
            }
        });
    }

    @Override
    public void updateView(TestBean expressInfo) {
        msgTv.setText(new Gson().toJson(expressInfo));
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void hideProgressDialog() {
        progressDialog.dismiss();
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.ok_btn)
    public void onViewClicked() {
        //mainPresenter.getTestInfo(1);
        // mainPresenter.getPostTestInfo(2);

        String path = Environment.getExternalStorageDirectory() + File.separator + "test.png";
        String path2 = Environment.getExternalStorageDirectory() + File.separator + "test2.png";
        mainPresenter.uploadFile("文件描述", new File(path), new File(path2));
    }
}
